﻿#include "map.h"
#include "ui_map.h"

#include <QDir>
#include <QWebEngineSettings>

Map::Map(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Map)
{
    ui->setupUi(this);
    bridge = new mapBridge(this);
    gotoMain = ui->gotoMain;
    InitMap();
    connect(bridge,&mapBridge::recRouteCoordinates,this,&Map::receiveRouteCoordinates);     //接收路线坐标集
    connect(bridge,&mapBridge::isLoad,this,[=](){


        setBoxPoint(122.08930442831216,37.538181449863764);//哈工大坐标
        setBoxPoint1(122.06528894004533,37.53830590145268);//山大坐标
        setWay(122.08930442831216,37.538181449863764,122.06528894004533,37.53830590145268);
        setDronePoint(122.0646860,37.5345050);
        setCarPoint(122.0801320,37.5332850);
        // setDronePoint(116.404, 39.915);
        // setBoxPoint(113.704, 36.215);
        // setCarPoint(110.004, 33.515);
        // setWay(116.404, 39.915,113.704, 36.215);
    });
}

Map::~Map()
{
    delete ui;
}

void Map::InitMap()
{
    page = ui->widget->page();
    page->settings()->setAttribute(QWebEngineSettings::LocalContentCanAccessRemoteUrls, true);
    page->settings()->setAttribute(QWebEngineSettings::LocalContentCanAccessFileUrls, true);
    page->settings()->setAttribute(QWebEngineSettings::JavascriptEnabled, true);
    page->settings()->setAttribute(QWebEngineSettings::WebGLEnabled, true); // 启用WebGL支持
    page->settings()->setAttribute(QWebEngineSettings::LocalContentCanAccessRemoteUrls, true);
    page->settings()->setAttribute(QWebEngineSettings::LocalStorageEnabled, true);

    channel = new QWebChannel(this);
    channel->registerObject("bridge",bridge);
    page->setWebChannel(channel);
    QString htmlPath = "qrc:/MAP/map.html";
    page->load(QUrl(htmlPath));
}

void Map::setBoxPoint(double lng, double lat)
{
    QString jsCode = QString("setBoxPoint(%1,%2);").arg(lng).arg(lat);
    if(page){
        page->runJavaScript(jsCode);
        qDebug()<<jsCode;
    }
}

void Map::setBoxPoint1(double lng, double lat)
{
    QString jsCode = QString("setBoxPoint1(%1,%2);").arg(lng).arg(lat);
    if(page){
        page->runJavaScript(jsCode);
        qDebug()<<jsCode;
    }
}

void Map::setCarPoint(double lng, double lat)
{
    QString jsCode = QString("setCurrentCarPoint(%1,%2);").arg(lng).arg(lat);
    if(page){
        page->runJavaScript(jsCode);
        qDebug()<<jsCode;
    }
}

void Map::setDronePoint(double lng, double lat)
{
    QString jsCode = QString("setCurrentDronePoint(%1,%2);").arg(lng).arg(lat);
    if(page){
        page->runJavaScript(jsCode);
        qDebug()<<jsCode;
    }
}

void Map::deleteBoxPoint()
{
    QString jsCode = QString("deleteBoxPoint();");
    if(page){
        page->runJavaScript(jsCode);
        qDebug()<<jsCode;
    }
}

void Map::deleteCarPoint()
{
    QString jsCode = QString("deleteCurrentCarPoint();");
    if(page){
        page->runJavaScript(jsCode);
        qDebug()<<jsCode;
    }
}

void Map::deleteDronePoint()
{
    QString jsCode = QString("deleteCurrentDronePoint();");
    if(page){
        page->runJavaScript(jsCode);
        qDebug()<<jsCode;
    }
}

void Map::setWay(double sLng, double sLat, double eLng, double eLat)
{
    QString jsCode = QString("setWay(%1,%2,%3,%4);").arg(sLng).arg(sLat).arg(eLng).arg(eLat);
    if(page){
        page->runJavaScript(jsCode);
        qDebug()<<jsCode;
    }
}

void Map::receiveRouteCoordinates(const QString &jsonStr)
{
    qDebug()<<jsonStr;
    QJsonDocument doc = QJsonDocument::fromJson(jsonStr.toUtf8());
    QVector<QPointF> points;
    foreach(const QJsonValue &val, doc.array()) {

        points.append(QPointF(
            val.toObject()["lng"].toDouble(),
            val.toObject()["lat"].toDouble()
            ));
    }

    qDebug() << "Received" << points.size() << "coordinates";
}

